package com.zhibang.util;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 后端统一JSON返回类
 */
@Data
@NoArgsConstructor
public class Result<T> {
    private Integer code; // 自定义状态码
    private String msg;   // 消息
    private T data;       // 数据

    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> Result<T> ok(){
        return new Result<>(200,"success");
    }

    public static <T> Result<T> ok(String msg){
        return new Result<>(200,msg);
    }

    public static <T> Result<T> data(T data){
        return new Result<>(200,"success",data);
    }

    public static <T> Result<T> fail(){
        return new Result<>(500,"error");
    }

    public static <T> Result<T> fail(String msg){
        return new Result<>(500,msg);
    }

    public static <T> Result<T> fail(Integer code,String msg){
        return new Result<>(code,msg);
    }
}
